package com.sherlochao.controller;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSON;
import com.sherlochao.model.Comment;
import com.sherlochao.model.Photo;
import com.sherlochao.model.User;
import com.sherlochao.pojo.CommentPOJO;
import com.sherlochao.service.CommentService;
import com.sherlochao.service.PhotoService;
import com.sherlochao.service.UserService;

/**
 * 评论回复
 * 
 * @author yinchao
 *
 */

@Controller
@RequestMapping("/comment")
public class CommentController {

	@Resource
	private CommentService commentService;

	@Resource
	private PhotoService photoService;

	@Resource
	private UserService userService;

	@ResponseBody
	@RequestMapping(value = "/sendComment", method = RequestMethod.POST)
	public void sendComment(
			String commentContent,
			String myUserId,
			String otherUserId,
			String shareTalkId,
			@RequestParam(value = "file", required = false) MultipartFile[] file,
			HttpServletRequest request) throws Exception {

		Comment comment = new Comment();
		comment.setCommentContent(commentContent);
		comment.setMyUserId(myUserId);
		comment.setOtherUserId(otherUserId);
		comment.setShareTalkId(shareTalkId);
		commentService.save(comment);

		String commentId = comment.getId();

		// 获得物理路径webapp所在路径
		String pathRoot = request.getSession().getServletContext()
				.getRealPath("");
		String path = "";
		for (MultipartFile mf : file) {
			if (!mf.isEmpty()) {
				// 生成uuid作为文件名称
				String uuid = UUID.randomUUID().toString().replaceAll("-", "");
				// 获得文件类型
				String contentType = mf.getContentType();
				// 获得文件后缀名称
				String name = contentType
						.substring(contentType.indexOf("/") + 1);
				if (name.equals("jpeg")) {
					path = "/Photo/" + uuid + "." + name;
					mf.transferTo(new File(pathRoot + path));
					Photo photo = new Photo();
					photo.setCommentId(commentId);
					photo.setPhotoAddress(pathRoot + path);
					photoService.save(photo);
				}
			}
		}

	}
	
	@ResponseBody
	@RequestMapping(value = "/getComment", method = RequestMethod.POST)
	public String getComment(String commentId){
		CommentPOJO commentPOJO = new CommentPOJO();
		Comment comment = commentService.get(commentId);
		commentPOJO.setCommentContent(comment.getCommentContent());
		User myUser = userService.get(comment.getMyUserId());
		commentPOJO.setMyUserName(myUser.getUsername());
		User otherUser = userService.get(comment.getOtherUserId());
		commentPOJO.setOtherUserName(otherUser.getUsername());
		commentPOJO.setShareTalkId(comment.getShareTalkId());
		List<Photo> photos = photoService.getPhotoByCommentId(commentId);
		if(photos != null){
			ArrayList<String> photoAddress = new ArrayList<String>();
			for(int i=0;i<photos.size();i++){
				photoAddress.add(photos.get(i).getPhotoAddress());
			}
			commentPOJO.setPhotoAddress(photoAddress);
		}
		String json = JSON.toJSONString(commentPOJO);
		System.out.println(json);
		return json;
	}

}
